import React, { Component } from "react";
import { Button } from "@ali/ding-ui";
import PropTypes from "prop-types";


import { alert as NativeAlert, openLink, actionSheet } from "../../utils/dingtalk.js";
export default class FooterButton extends Component {
  constructor(props) {
    super(props);
  }
  static propTypes = {
    isAdmin: PropTypes.bool,
    employeeStatus: PropTypes.string,
    username: PropTypes.string,
    onRegularClick: PropTypes.func,
    onHandoverClick: PropTypes.func,
    abort: PropTypes.func,
    delete: PropTypes.func,
    leaveJob: PropTypes.func,
    approvalVO: PropTypes.object
  };

  showActionSheet(employeeStatus, username) {
    const buttonMapper = {
      "2": [
        {
          text: "提醒员工发起转正",
          onClick: this.onButtonClick.bind(this, employeeStatus, username)
        },
        {
          text: "办理离职",
          onClick: this.props.leaveJob
        }
      ],
      "3": [
        {
          text: "办理离职",
          onClick: this.props.leaveJob
        }
      ],
      "5": [
        {
          text: "确认离职",
          onClick: this.props.delete
        },
        {
          text: "撤销离职",
          onClick: this.props.abort
        },
        {
          text: "提醒离职交接",
          onClick: this.onButtonClick.bind(this, employeeStatus, username)
        }
      ]
    };

    const actions = buttonMapper[employeeStatus];
    const BUTTONS = actions.map(q => q.text);

    actionSheet({
      title:"请选择人事操作",
      cancelButton: "取消", //取消按钮文本
      otherButtons: BUTTONS,
      onSuccess: ({ buttonIndex }) => {
        actions[buttonIndex].onClick();
      }
    });
  }
  onButtonClick(employeeStatus, username) {
    const approvalVO = this.props.approvalVO || {};
    // 是否开启转正审批套件
    const enableRegular = approvalVO.enableRegular;
    // 开启转正审批套件的地址
    const regularUrl = approvalVO.regularUrl;
    // 是否开启离职审批套件
    const enableDismissionHandover = approvalVO.enableDismissionHandover;
    // 开启离职审批套件的地址
    const dismissionHandoverUrl = approvalVO.dismissionHandoverUrl;

    const actionMapper = {
      "2": this.props.onRegularClick,
      "5": this.props.onHandoverClick
    };

    if (employeeStatus === "2" && !enableRegular) {
      return NativeAlert({
        message: "转正审批未开通，不能发送消息",
        buttonName: "立即开通",
        onSuccess() {
          openLink(regularUrl);
        }
      });
    } else if (employeeStatus === "5" && !enableDismissionHandover) {
      return NativeAlert({
        message: "离职交接审批未开通",
        buttonName: "立即开通",
        onSuccess() {
          openLink(dismissionHandoverUrl);
        }
      });
    }
    actionMapper[employeeStatus]();
  }
  render() {
    const { isAdmin, employeeStatus, username } = this.props;
    if (employeeStatus === "4") return null;
    if (!isAdmin) return null;

    return (
      <div className="scroll-btn scroll-btn-preview">
        <Button
          className="preview-button"
          type="primary"
          onClick={() => this.showActionSheet(employeeStatus, username)}
        >
          操作
        </Button>
      </div>
    );
  }
}
